package com.kurumi.medical.service.impl;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kurumi.medical.component.MyConfig;
import com.kurumi.medical.datasource.TargetDataSource;
import com.kurumi.medical.mapper.CitizensInfoMapper;
import com.kurumi.medical.mapper.ExportDataMapper;
import com.kurumi.medical.mapper.FirstLevelMedicalMapper;
import com.kurumi.medical.mapper.HospitalizationNoteMapper;
import com.kurumi.medical.model.HospitalVisitBasicInfo;
import com.kurumi.medical.model.HospitalVisitCostInfo;
import com.kurumi.medical.model.HospitalVisitCureInfo;
import com.kurumi.medical.model.HospitalVisitCureWorker;
import com.kurumi.medical.model.HospitalVisitDiagAccordRecord;
import com.kurumi.medical.model.HospitalVisitDiseaseDiagInfo;
import com.kurumi.medical.model.HospitalVisitDiseaseDiagRecord;
import com.kurumi.medical.model.HospitalVisitDoctorAdviceTemporaryInfo;
import com.kurumi.medical.model.HospitalVisitDoctorAdviceTemporaryItem;
import com.kurumi.medical.model.HospitalVisitDoctorAdviceTemporaryRecord;
import com.kurumi.medical.model.HospitalVisitExaminationReportsInfo;
import com.kurumi.medical.model.HospitalVisitExaminationReportsItem;
import com.kurumi.medical.model.HospitalVisitExaminationReportsRecord;
import com.kurumi.medical.model.HospitalVisitInfusionBloodElement;
import com.kurumi.medical.model.HospitalVisitInfusionBloodInfo;
import com.kurumi.medical.model.HospitalVisitInfusionBloodRecord;
import com.kurumi.medical.model.HospitalVisitIntensiveCareInfo;
import com.kurumi.medical.model.HospitalVisitNurseInfo;
import com.kurumi.medical.model.HospitalVisitOperateInfo;
import com.kurumi.medical.model.HospitalVisitOperateRecord;
import com.kurumi.medical.model.HospitalVisitOperateWorker;
import com.kurumi.medical.model.HospitalVisitPressureSoreInfo;
import com.kurumi.medical.model.HospitalVisitTransferDeptInfo;
import com.kurumi.medical.model.HospitalVisitTransferDeptRecord;
import com.kurumi.medical.pojo.CitizensInfo;
import com.kurumi.medical.pojo.FirstLevelMedical;
import com.kurumi.medical.pojo.HospitalizationNote;
import com.kurumi.medical.service.HospitalVisitService;
import com.kurumi.medical.util.DateUtil;
import com.kurumi.medical.util.FileUtil;
import com.kurumi.medical.util.JsonUtil;
import com.kurumi.medical.util.StringUtil;

@Service
public class HospitalVisitServiceImpl implements HospitalVisitService {

	private String idDocumentTypeCode = "SFZ";
	
	private String hospitalCode = "49557184-0";
	
	@Autowired
	private MyConfig myConfig;
	
	
	@Autowired
	private ExportDataMapper exportDataMapper;
	
	@Autowired
	private CitizensInfoMapper patientMapper;
	
	@Autowired
	private FirstLevelMedicalMapper firstLevelMedicalMapper;
	
	@Autowired
	private HospitalizationNoteMapper hospitalizationNoteMapper;
	
	
	@Override
	public List<Map<String, Object>> getDatasByOracle() {
		// TODO Auto-generated method stub
		List<Map<String, Object>> hospitalVisitFillInfos = getHospitalVisitFillInfoByOracle();
		return hospitalVisitFillInfos;
	}

	private List<Map<String, Object>> getHospitalVisitFillInfoByOracle(){
		List<Map<String, Object>> hospitalVisitInfos = exportDataMapper.getHospitalVisitInfoByOracle();
		List<Map<String, Object>> hospitalVisitFillInfos = new ArrayList<Map<String,Object>>();
		for(Map<String, Object> hospitalVisitInfo : hospitalVisitInfos){
			for (String key : hospitalVisitInfo.keySet()) {
				Object value = hospitalVisitInfo.get(key);
			
			
				if(value instanceof String){
					String dealValue = null;
					try {
						dealValue = new String(((String)value).getBytes("ISO8859-1"),"gbk");
					} catch (UnsupportedEncodingException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					hospitalVisitInfo.put(key, dealValue);
				}
			}
			Map<String, Object> hospitalVisitFillInfo = new HashMap<String, Object>();
			String visitGuid = StringUtil.getId();
			HospitalVisitBasicInfo basicInfo = new HospitalVisitBasicInfo();
			basicInfo.setVisitGuid(visitGuid);
			basicInfo.setPatientName((String)hospitalVisitInfo.get("PATIENTNAME"));
			basicInfo.setSexCode((String)hospitalVisitInfo.get("SEXCODE"));
			basicInfo.setSexName((String)hospitalVisitInfo.get("SEXNAME"));
			String birthdayStr = StringUtil.meaningStr((String)hospitalVisitInfo.get("BIRTHDAY"));
			if(birthdayStr != null){
				try {
					Date birthday = DateUtil.dateParse(birthdayStr);
					basicInfo.setBirthday(birthday);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			basicInfo.setHospitalCode(hospitalCode);
			basicInfo.setIdDocumentTypeCode(idDocumentTypeCode);
			basicInfo.setIdDocumentTypeName("身份证");
			basicInfo.setIdNumber((String)hospitalVisitInfo.get("IDNUMBER"));
			basicInfo.setNationalityCode((String)hospitalVisitInfo.get("NATIONALITYCODE"));
			basicInfo.setNationalityName((String)hospitalVisitInfo.get("NATIONALITYNAME"));
			basicInfo.setNationCode((String)hospitalVisitInfo.get("NATIONCODE"));
			basicInfo.setNationName((String)hospitalVisitInfo.get("NATIONNAME"));
			basicInfo.setBirthAddressCode((String)hospitalVisitInfo.get("BIRTHADDRESSCODE"));
			basicInfo.setBirthAddress((String)hospitalVisitInfo.get("BIRTHADDRESS"));
			
			basicInfo.setMarriageCode((String)hospitalVisitInfo.get("MARRIAGECODE"));
			basicInfo.setMarriageName((String)hospitalVisitInfo.get("MARRIAGENAME"));
			
			basicInfo.setProfessionCode((String)hospitalVisitInfo.get("PROFESSIONCODE"));
			basicInfo.setProfessionName((String)hospitalVisitInfo.get("PROFESSIONNAME"));
			
			basicInfo.setPermanentAddress((String)hospitalVisitInfo.get("PERMANENTADDRESS"));
			basicInfo.setPermanentAddressPostCode((String)hospitalVisitInfo.get("PERMANENTADDRESSPOSTCODE"));
			
			basicInfo.setLinkManName((String)hospitalVisitInfo.get("LINKMANNAME"));
			basicInfo.setLinkManAddress((String)hospitalVisitInfo.get("LINKMANADDRESS"));
			
			basicInfo.setLinkManPhone((String)hospitalVisitInfo.get("LINKMANPHONE"));
			basicInfo.setLinkManRelativeRelationCode((String)hospitalVisitInfo.get("LINKMANRELATIVERELATIONCODE"));
			basicInfo.setLinkManRelativeRelationName((String)hospitalVisitInfo.get("LINKMANRELATIVERELATIONNAME"));
			
			basicInfo.setOnlyId((String)hospitalVisitInfo.get("ONLYID"));
			basicInfo.setMrId((String)hospitalVisitInfo.get("MRID"));
			BigDecimal visitNumber = (BigDecimal)hospitalVisitInfo.get("VISITNUMBER");
			if(visitNumber!= null){
				basicInfo.setVisitNumber(visitNumber);
			}
			
			String inHospitalDateTimeStr = StringUtil.meaningStr((String)hospitalVisitInfo.get("INHOSPITALNDATETIME"));
			if(inHospitalDateTimeStr != null){
				try {
					Date inHospitalDateTime = DateUtil.dateParse(DateUtil.DATE_TIME_FORMATE,inHospitalDateTimeStr);
					basicInfo.setInHospitalDateTime(inHospitalDateTime);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			basicInfo.setInDeptCode((String)hospitalVisitInfo.get("INDEPTCODE"));
			basicInfo.setInDeptName((String)hospitalVisitInfo.get("INDEPTNAME"));
			basicInfo.setOutDeptCode((String)hospitalVisitInfo.get("OUTDEPTCODE"));
			basicInfo.setOutDeptName((String)hospitalVisitInfo.get("OUTDEPTNAME"));
			
			String outHospitalDateTimeStr = StringUtil.meaningStr((String)hospitalVisitInfo.get("OUTHOSPITALNDATETIME"));
			if(outHospitalDateTimeStr != null){
				try {
					Date outHospitalDateTime = DateUtil.dateParse(DateUtil.DATE_TIME_FORMATE,outHospitalDateTimeStr);
					basicInfo.setOutHospitalDateTime(outHospitalDateTime);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			basicInfo.setOutHospitalTypeCode((String)hospitalVisitInfo.get("OUTHOSPITALTYPECODE"));
			basicInfo.setOutHospitalTypeName((String)hospitalVisitInfo.get("OUTHOSPITALTYPENAME"));
			
			basicInfo.setWorkUnitName((String)hospitalVisitInfo.get("WORKUNITNAME"));
			
			String clinicalReceptionDateTimeStr = StringUtil.meaningStr((String)hospitalVisitInfo.get("CLINICALRECEPTIONDATETIME"));
			if(clinicalReceptionDateTimeStr != null){
				try {
					Date clinicalReceptionDateTime = DateUtil.dateParse(DateUtil.DATE_TIME_FORMATE,clinicalReceptionDateTimeStr);
					basicInfo.setClinicalReceptionDateTime(clinicalReceptionDateTime);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			basicInfo.setOutpatientOfDoctorName((String)hospitalVisitInfo.get("OUTPATIENTOFDOCTORNAME"));
			
			basicInfo.setResponsiblePersonName((String)hospitalVisitInfo.get("RESPONSIBLEPERSONNAME"));
			
			basicInfo.setInHospitalTypeCode((String)hospitalVisitInfo.get("INHOSPITALTYPECODE"));
			
			basicInfo.setInHospitalTypeName((String)hospitalVisitInfo.get("INHOSPITALTYPENAME"));
			
			basicInfo.setInHospitalStateCode((String)hospitalVisitInfo.get("INHOSPITALSTATECODE"));
			
			basicInfo.setInHospitalStateName((String)hospitalVisitInfo.get("INHOSPITALSTATENAME"));
			
			basicInfo.setAutopsyCode((String)hospitalVisitInfo.get("AUTOPSYCODE"));
			basicInfo.setAutopsyName((String)hospitalVisitInfo.get("AUTOPSYNAME"));
			
			HospitalVisitCureInfo cureInfo = new HospitalVisitCureInfo();
			cureInfo.setVisitGuid(visitGuid);
			BigDecimal rescueNumber = (BigDecimal)hospitalVisitInfo.get("RESCUENUMBER");
			if(rescueNumber != null){
				cureInfo.setRescueNumber(rescueNumber.intValue());
			}
			
			BigDecimal rescueSucceedNumber = (BigDecimal)hospitalVisitInfo.get("RESCUESUCCEEDNUMBER");
			if(rescueSucceedNumber != null){
				cureInfo.setRescueSucceedNumber(rescueSucceedNumber.intValue());
			}
			
			cureInfo.setBloodTypeCode((String)hospitalVisitInfo.get("BLOODTYPECODE"));
			cureInfo.setBloodTypeName((String)hospitalVisitInfo.get("BLOODTYPENAME"));
			cureInfo.setRhBloodTypeCode((String)hospitalVisitInfo.get("RHBLOODTYPECODE"));
			cureInfo.setRhBloodTypeName((String)hospitalVisitInfo.get("RHBLOODTYPENAME"));
			
			BigDecimal infusionTimes = (BigDecimal)hospitalVisitInfo.get("INFUSIONTIMES");
			if(infusionTimes != null){
				cureInfo.setInfusionTimes(infusionTimes.intValue());
			}
			
			cureInfo.setFollowUpClinicLimitCode((String)hospitalVisitInfo.get("FOLLOWUPCLINICLIMITCODE"));
			cureInfo.setFollowUpClinicLimitName((String)hospitalVisitInfo.get("FOLLOWUPCLINICLIMITNAME"));
			
			
			cureInfo.setFollowUpClinicDayNumber((BigDecimal)hospitalVisitInfo.get("FOLLOWINTERVAL"));
			
			List<HospitalVisitCureWorker> cureWorkers = new ArrayList<HospitalVisitCureWorker>();
			HospitalVisitCureWorker cureWorker1 = new HospitalVisitCureWorker();
			cureWorker1.setProfessionTitleName("科主任");
			cureWorker1.setMedicalWorkerName((String)hospitalVisitInfo.get("DIRECTOR"));
			cureWorkers.add(cureWorker1);
			HospitalVisitCureWorker cureWorker2 = new HospitalVisitCureWorker();
			cureWorker2.setProfessionTitleName("主治医师");
			cureWorker2.setMedicalWorkerName((String)hospitalVisitInfo.get("ATTENDING_DOCTOR"));
			cureWorkers.add(cureWorker2);
			
			HospitalVisitCureWorker cureWorker3 = new HospitalVisitCureWorker();
			cureWorker3.setProfessionTitleName("住院医师");
			cureWorker3.setMedicalWorkerName((String)hospitalVisitInfo.get("DOCTOR_IN_CHARGE"));
			cureWorkers.add(cureWorker3);
			cureInfo.setCureWorkers(cureWorkers);
			
			
			List<Map<String, Object>> hospitalVisitDiagCompareInfos = exportDataMapper.getHospitalVisitDiagCompareInfoByOracle(basicInfo.getMrId(),basicInfo.getVisitNumber().intValue());
			List<HospitalVisitDiagAccordRecord> diagAccordRecords = new ArrayList<HospitalVisitDiagAccordRecord>();
			for (Map<String, Object> hospitalVisitDiagCompareInfo : hospitalVisitDiagCompareInfos) {
				for (String key : hospitalVisitDiagCompareInfo.keySet()) {
					Object value = hospitalVisitDiagCompareInfo.get(key);
				
				
					if(value instanceof String){
						String dealValue = null;
						try {
							dealValue = new String(((String)value).getBytes("ISO8859-1"),"gbk");
						} catch (UnsupportedEncodingException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						hospitalVisitDiagCompareInfo.put(key, dealValue);
					}
				}
				
				HospitalVisitDiagAccordRecord diagAccordRecord = new HospitalVisitDiagAccordRecord();
				diagAccordRecord.setDiagCompareCode((String)hospitalVisitDiagCompareInfo.get("DIAGCOMPARECODE"));
				diagAccordRecord.setDiagCompareName((String)hospitalVisitDiagCompareInfo.get("DIAGCOMPARENAME"));
				diagAccordRecord.setDiagAccordCode((String)hospitalVisitDiagCompareInfo.get("DIAGACCORDCODE"));
				diagAccordRecord.setDiagAccordName((String)hospitalVisitDiagCompareInfo.get("DIAGACCORDNAME"));
				diagAccordRecords.add(diagAccordRecord);
			}
			cureInfo.setDiagAccordRecords(diagAccordRecords);
			HospitalVisitNurseInfo nurseInfo = new HospitalVisitNurseInfo();
			nurseInfo.setVisitGuid(visitGuid);
			nurseInfo.setSickDayNumber((BigDecimal)hospitalVisitInfo.get("SICKDAYNUMBER"));
			nurseInfo.setCriticalDayNumber((BigDecimal)hospitalVisitInfo.get("CRITICALDAYNUMBER"));
			HospitalVisitIntensiveCareInfo intensiveCareInfo = new HospitalVisitIntensiveCareInfo();
			intensiveCareInfo.setVisitGuid(visitGuid);
			intensiveCareInfo.setIcuNurseDayNumber((BigDecimal)hospitalVisitInfo.get("ICUNURSEDAYNUMBER"));
			intensiveCareInfo.setCcuNurseDayNumber((BigDecimal)hospitalVisitInfo.get("CCUNURSEDAYNUMBER"));
			
			nurseInfo.setSpecialNurseDayNumber((BigDecimal)hospitalVisitInfo.get("SPECIALNURSEDAYNUMBER"));
			nurseInfo.setFirstLevelNurseDayNumber((BigDecimal)hospitalVisitInfo.get("FIRSTLEVELNURSEDAYNUMBER"));
			nurseInfo.setSecondLevelNurseDayNumber((BigDecimal)hospitalVisitInfo.get("SECONDLEVELNURSEDAYNUMBER"));
			
			HospitalVisitInfusionBloodInfo infusionBloodInfo = new HospitalVisitInfusionBloodInfo();
			BigDecimal infusionBloodTimes = (BigDecimal)hospitalVisitInfo.get("SECONDLEVELNURSEDAYNUMBER");
			if(infusionBloodTimes != null){
				infusionBloodInfo.setInfusionBloodTimes(infusionBloodTimes.intValue());
			}
			infusionBloodInfo.setInfusionBloodTotalVolume((BigDecimal)hospitalVisitInfo.get("INFUSIONBLOODTOTALVOLUME"));
			
			BigDecimal infusionBloodReactTimes = (BigDecimal)hospitalVisitInfo.get("INFUSIONBLOODREACTTIMES");
			if(infusionBloodReactTimes != null){
				infusionBloodInfo.setInfusionBloodReactTimes(infusionBloodReactTimes.intValue());
			}
			
			HospitalVisitPressureSoreInfo pressureSoreInfo = new HospitalVisitPressureSoreInfo();
			pressureSoreInfo.setPressureSoreTimes((BigDecimal)hospitalVisitInfo.get("PRESSURESORETIMES"));
			
			HospitalVisitCostInfo visitCostInfo = new HospitalVisitCostInfo();
			visitCostInfo.setTotalCost((BigDecimal)hospitalVisitInfo.get("TOTALCOST"));
			visitCostInfo.setActuallyCost((BigDecimal)hospitalVisitInfo.get("ACTUALLYCOST"));
			
			cureInfo.setMedicalRecordQualityCode((String)hospitalVisitInfo.get("MEDICALRECORDQUALITYCODE"));
			cureInfo.setMedicalRecordQualityName((String)hospitalVisitInfo.get("MEDICALRECORDQUALITYNAME"));
			
			basicInfo.setMajorUnitCode((String)hospitalVisitInfo.get("MAJORUNITCODE"));
			basicInfo.setMajorUnitName((String)hospitalVisitInfo.get("MAJORUNITNAME"));
			
			basicInfo.setTroopServiceCode((String)hospitalVisitInfo.get("TROOPSERVICECODE"));
			basicInfo.setTroopServiceName((String)hospitalVisitInfo.get("TROOPSERVICENAME"));
			
			BigDecimal medicalSystemCode = (BigDecimal)hospitalVisitInfo.get("MEDICALSYSTEMCODE");
			if(medicalSystemCode != null){
				basicInfo.setMedicalSystemCode(medicalSystemCode.intValue()+"");
			}
			basicInfo.setMedicalSystemName((String)hospitalVisitInfo.get("MEDICALSYSTEMNAME"));
			basicInfo.setIdTypeName((String)hospitalVisitInfo.get("IDTYPENAME"));
			basicInfo.setIdTypeCode((String)hospitalVisitInfo.get("IDTYPECODE"));
			
			basicInfo.setCostTypeName((String)hospitalVisitInfo.get("COSTTYPENAME"));
			basicInfo.setCostTypeCode((String)hospitalVisitInfo.get("COSTTYPECODE"));
			
			BigDecimal jobStateCode = (BigDecimal)hospitalVisitInfo.get("JOBSTATECODE");
			if(jobStateCode != null){
				basicInfo.setJobStateCode(jobStateCode.intValue()+"");
			}
			
			basicInfo.setJobStateName((String)hospitalVisitInfo.get("JOBSTATENAME"));
			
			List<Map<String, Object>> hospitalVisitDiagInfos = exportDataMapper.getHospitalVisitDiagInfoByOracle(basicInfo.getMrId(),basicInfo.getVisitNumber().intValue());
			
			
			HospitalVisitDiseaseDiagInfo diseaseDiagInfo = new HospitalVisitDiseaseDiagInfo();
			diseaseDiagInfo.setVisitGuid(visitGuid);
			List<HospitalVisitDiseaseDiagRecord> diseaseDiagRecords = new ArrayList<HospitalVisitDiseaseDiagRecord>();
			for (Map<String, Object> hospitalVisitDiagInfo : hospitalVisitDiagInfos) {
				for (String key : hospitalVisitDiagInfo.keySet()) {
					Object value = hospitalVisitDiagInfo.get(key);
				
				
					if(value instanceof String){
						String dealValue = null;
						try {
							dealValue = new String(((String)value).getBytes("ISO8859-1"),"gbk");
						} catch (UnsupportedEncodingException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						hospitalVisitDiagInfo.put(key, dealValue);
					}
				}
				HospitalVisitDiseaseDiagRecord diseaseDiagRecord = new HospitalVisitDiseaseDiagRecord();
				diseaseDiagRecord.setDiagTypeCode((String)hospitalVisitDiagInfo.get("DIAGTYPECODE"));
				diseaseDiagRecord.setDiagTypeName((String)hospitalVisitDiagInfo.get("DIAGTYPENAME"));
				diseaseDiagRecord.setDiseaseDiagCode((String)hospitalVisitDiagInfo.get("DISEASEDIAGCODE"));
				diseaseDiagRecord.setDiseaseDiagName((String)hospitalVisitDiagInfo.get("DISEASEDIAGNAME"));
				String confirmedDateTimeStr = (String)hospitalVisitDiagInfo.get("CONFIRMEDDATETIME");
				if(confirmedDateTimeStr != null){
					try {
						Date confirmedDateTime = DateUtil.dateParse(DateUtil.DATE_TIME_FORMATE,confirmedDateTimeStr);
						diseaseDiagRecord.setConfirmedDateTime(confirmedDateTime);
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				diseaseDiagRecord.setTreatResultCode((String)hospitalVisitDiagInfo.get("TREATRESULTCODE"));
				diseaseDiagRecord.setTreatResultName((String)hospitalVisitDiagInfo.get("TREATRESULTNAME"));
				BigDecimal diagSortIndex = (BigDecimal)hospitalVisitDiagInfo.get("DIAGSORTINDEX");
				if(diagSortIndex != null){
					diseaseDiagRecord.setDiagSortIndex(diagSortIndex.intValue());
				}
				diseaseDiagRecord.setTreatDayNumber((BigDecimal)hospitalVisitDiagInfo.get("TREATDAYNUMBER"));
				diseaseDiagRecords.add(diseaseDiagRecord);
			}
			diseaseDiagInfo.setDiseaseDiagRecords(diseaseDiagRecords);
			
			List<Map<String, Object>> hospitalVisitOperateInfos = exportDataMapper.getHospitalVisitOperateInfoByOracle(basicInfo.getMrId(),basicInfo.getVisitNumber().intValue());
			
			
			HospitalVisitOperateInfo operateInfo = new HospitalVisitOperateInfo();
			operateInfo.setVisitGuid(visitGuid);
			List<HospitalVisitOperateRecord> operateRecords = new ArrayList<HospitalVisitOperateRecord>();
			for (Map<String, Object> hospitalVisitOperateInfo : hospitalVisitOperateInfos) {
				for (String key : hospitalVisitOperateInfo.keySet()) {
					Object value = hospitalVisitOperateInfo.get(key);
					if(value instanceof String){
						String dealValue = null;
						try {
							dealValue = new String(((String)value).getBytes("ISO8859-1"),"gbk");
						} catch (UnsupportedEncodingException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						hospitalVisitOperateInfo.put(key, dealValue);
					}
				}
				
				HospitalVisitOperateRecord operateRecord = new HospitalVisitOperateRecord();
				operateRecord.setOperateCode((String)hospitalVisitOperateInfo.get("OPERATECODE"));
				operateRecord.setOperateName((String)hospitalVisitOperateInfo.get("OPERATENAME"));
				BigDecimal opsIndex = (BigDecimal)hospitalVisitOperateInfo.get("OPSINDEX");
				if(opsIndex != null){
					operateRecord.setOperateSortIndex(opsIndex.intValue());
				}
				
				operateRecord.setIncisionLevelCode((String)hospitalVisitOperateInfo.get("INCISIONLEVELCODE"));
				operateRecord.setIncisionLevelName((String)hospitalVisitOperateInfo.get("INCISIONLEVELNAME"));
				operateRecord.setIncisionLevelCode((String)hospitalVisitOperateInfo.get("CICATRIZETYPECODE"));
				operateRecord.setIncisionLevelName((String)hospitalVisitOperateInfo.get("CICATRIZETYPENAME"));
				String operateDateStr = (String)hospitalVisitOperateInfo.get("OPERATEDATE");
				if(operateDateStr != null){
					try {
						Date operateDate = DateUtil.dateParse(DateUtil.DATE_TIME_FORMATE,operateDateStr);
						operateRecord.setOperateStartDate(operateDate);
						operateRecord.setOperateEndDate(operateDate);
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				operateRecord.setAnaesthesiaTypeCode((String)hospitalVisitOperateInfo.get("ANAESTHESIATYPECODE"));
				operateRecord.setAnaesthesiaTypeName((String)hospitalVisitOperateInfo.get("ANAESTHESIATYPENAME"));
				List<HospitalVisitOperateWorker> operateWorkers = new ArrayList<HospitalVisitOperateWorker>();
				HospitalVisitOperateWorker operateWorker1 = new HospitalVisitOperateWorker();
				operateWorker1.setProfessionTitleName("手术医师");
				operateWorker1.setMedicalWorkerName((String)hospitalVisitOperateInfo.get("OPERATOR"));
				operateWorkers.add(operateWorker1);
				
				HospitalVisitOperateWorker operateWorker2 = new HospitalVisitOperateWorker();
				operateWorker2.setProfessionTitleName("第一助手");
				operateWorker2.setMedicalWorkerName((String)hospitalVisitOperateInfo.get("FIRST_ASSISTANT"));
				operateWorkers.add(operateWorker2);
				
				HospitalVisitOperateWorker operateWorker3 = new HospitalVisitOperateWorker();
				operateWorker3.setProfessionTitleName("第二助手");
				operateWorker3.setMedicalWorkerName((String)hospitalVisitOperateInfo.get("SECOND_ASSISTANT"));
				operateWorkers.add(operateWorker3);
				
				HospitalVisitOperateWorker operateWorker4 = new HospitalVisitOperateWorker();
				operateWorker4.setProfessionTitleName("第一助手");
				operateWorker4.setMedicalWorkerName((String)hospitalVisitOperateInfo.get("ANESTHESIA_DOCTOR"));
				operateWorkers.add(operateWorker4);
				operateRecord.setOperateWorkers(operateWorkers);
				operateRecords.add(operateRecord);
			}
			operateInfo.setOperateRecords(operateRecords);
			
			List<Map<String, Object>> hospitalVisitTransferDeptInfos = exportDataMapper.getHospitalVisitTransferDeptInfoByOracle(basicInfo.getMrId(),basicInfo.getVisitNumber().intValue());
			
			
			HospitalVisitTransferDeptInfo transferDeptInfo = new HospitalVisitTransferDeptInfo();
			transferDeptInfo.setVisitGuid(visitGuid);
			List<HospitalVisitTransferDeptRecord> transferDeptRecords = new ArrayList<HospitalVisitTransferDeptRecord>();
			for (Map<String, Object> hospitalVisitTransferDeptInfo : hospitalVisitTransferDeptInfos) {
				for (String key : hospitalVisitTransferDeptInfo.keySet()) {
					Object value = hospitalVisitTransferDeptInfo.get(key);
					if(value instanceof String){
						String dealValue = null;
						try {
							dealValue = new String(((String)value).getBytes("ISO8859-1"),"gbk");
						} catch (UnsupportedEncodingException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						hospitalVisitTransferDeptInfo.put(key, dealValue);
					}
				}
				HospitalVisitTransferDeptRecord transferDeptRecord = new HospitalVisitTransferDeptRecord();
				transferDeptRecord.setOutDeptCode((String)hospitalVisitTransferDeptInfo.get("OUTDEPTCODE"));
				transferDeptRecord.setOutDeptName((String)hospitalVisitTransferDeptInfo.get("OUTDEPTNAME"));
				
				transferDeptRecord.setInDeptCode((String)hospitalVisitTransferDeptInfo.get("INDEPTCODE"));
				transferDeptRecord.setInDeptName((String)hospitalVisitTransferDeptInfo.get("INDEPTNAME"));
				String transferDeptInDateTimeStr = (String)hospitalVisitTransferDeptInfo.get("TRANSFERDEPTINDATETIME");
				if(transferDeptInDateTimeStr != null){
					if(transferDeptInDateTimeStr != null){
						try {
							Date transferDeptInDateTime = DateUtil.dateParse(DateUtil.DATE_TIME_FORMATE,transferDeptInDateTimeStr);
							transferDeptRecord.setTransferDeptInDateTime(transferDeptInDateTime);
							transferDeptRecord.setTransferDeptDateTime(transferDeptInDateTime);
						} catch (ParseException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
				String transferDeptOutDateTimeStr = (String)hospitalVisitTransferDeptInfo.get("TRANSFERDEPTOUTDATETIME");
				if(transferDeptOutDateTimeStr != null){
					try {
						Date transferDeptOutDateTime = DateUtil.dateParse(DateUtil.DATE_TIME_FORMATE,transferDeptOutDateTimeStr);
						transferDeptRecord.setTransferDeptOutDateTime(transferDeptOutDateTime);
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				transferDeptRecords.add(transferDeptRecord);
			}
			transferDeptInfo.setTransferDeptRecords(transferDeptRecords);
			List<Map<String, Object>> hospitalVisitTransferBloodInfos = exportDataMapper.getHospitalVisitTransferBloodInfoByOracle(basicInfo.getMrId(),basicInfo.getVisitNumber().intValue());
			
			
			List<HospitalVisitInfusionBloodRecord> infusionBloodRecords = new ArrayList<HospitalVisitInfusionBloodRecord>();
			
	/*		for (Map<String, Object> hospitalVisitTransferBloodInfo : hospitalVisitTransferBloodInfos) {
				for (String key : hospitalVisitTransferBloodInfo.keySet()) {
					Object value = hospitalVisitTransferBloodInfo.get(key);
					if(value instanceof String){
						String dealValue = null;
						try {
							dealValue = new String(((String)value).getBytes("ISO8859-1"),"gbk");
						} catch (UnsupportedEncodingException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						hospitalVisitTransferBloodInfo.put(key, dealValue);
					}
				}
				HospitalVisitInfusionBloodRecord infusionBloodRecord = new HospitalVisitInfusionBloodRecord();
				List<HospitalVisitInfusionBloodElement> bloodElements = new ArrayList<HospitalVisitInfusionBloodElement>();
				HospitalVisitInfusionBloodElement bloodElement1 = new HospitalVisitInfusionBloodElement();
				bloodElement1.setBloodElementName("全血");
				bloodElement1.setBloodElementVolume((BigDecimal)hospitalVisitTransferBloodInfo.get("WHOLE_BLOOD"));
				bloodElements.add(bloodElement1);
				
				HospitalVisitInfusionBloodElement bloodElement2 = new HospitalVisitInfusionBloodElement();
				bloodElement2.setBloodElementName("红细胞");
				bloodElement2.setBloodElementVolume((BigDecimal)hospitalVisitTransferBloodInfo.get("RED_CELL"));
				bloodElements.add(bloodElement2);
				
				
				HospitalVisitInfusionBloodElement bloodElement3 = new HospitalVisitInfusionBloodElement();
				bloodElement3.setBloodElementName("去除红细胞");
				bloodElement3.setBloodElementVolume((BigDecimal)hospitalVisitTransferBloodInfo.get("PURE_RED_CELL"));
				bloodElements.add(bloodElement3);
				
				HospitalVisitInfusionBloodElement bloodElement4 = new HospitalVisitInfusionBloodElement();
				bloodElement4.setBloodElementName("血小板");
				bloodElement4.setBloodElementVolume((BigDecimal)hospitalVisitTransferBloodInfo.get("PLATELET"));
				bloodElements.add(bloodElement4);
				
				HospitalVisitInfusionBloodElement bloodElement5 = new HospitalVisitInfusionBloodElement();
				bloodElement5.setBloodElementName("冷沉淀");
				bloodElement5.setBloodElementVolume((BigDecimal)hospitalVisitTransferBloodInfo.get("CRYOAGGLUTININ"));
				bloodElements.add(bloodElement5);
				
				HospitalVisitInfusionBloodElement bloodElement6 = new HospitalVisitInfusionBloodElement();
				bloodElement6.setBloodElementName("血浆");
				bloodElement6.setBloodElementVolume((BigDecimal)hospitalVisitTransferBloodInfo.get("PLASMA"));
				bloodElements.add(bloodElement6);
				
				HospitalVisitInfusionBloodElement bloodElement7 = new HospitalVisitInfusionBloodElement();
				bloodElement7.setBloodElementName("自身输血");
				bloodElement7.setBloodElementVolume((BigDecimal)hospitalVisitTransferBloodInfo.get("AUTOTRANSFUSION"));
				bloodElements.add(bloodElement7);
				
				HospitalVisitInfusionBloodElement bloodElement8 = new HospitalVisitInfusionBloodElement();
				bloodElement8.setBloodElementName("其他");
				bloodElement8.setBloodElementVolume((BigDecimal)hospitalVisitTransferBloodInfo.get("OTHER"));
				bloodElements.add(bloodElement8);
				infusionBloodRecord.setBloodElements(bloodElements);
				infusionBloodRecords.add(infusionBloodRecord);
			}*/
			infusionBloodInfo.setInfusionBloodRecords(infusionBloodRecords);
			
			
			List<Map<String, Object>> hospitalVisitDoctorAdviceInfos = exportDataMapper.getHospitalVisitDoctorAdviceInfoByOracle(basicInfo.getMrId(),basicInfo.getVisitNumber().intValue());
			
			HospitalVisitDoctorAdviceTemporaryInfo doctorAdviceTemporaryInfo = new HospitalVisitDoctorAdviceTemporaryInfo();
			doctorAdviceTemporaryInfo.setVisitGuid(visitGuid);
			Map<String, HospitalVisitDoctorAdviceTemporaryRecord> doctorAdviceTemporaryRecordMap = new HashMap<String, HospitalVisitDoctorAdviceTemporaryRecord>();
			for (Map<String, Object> hospitalVisitDoctorAdviceInfo : hospitalVisitDoctorAdviceInfos) {
				for (String key : hospitalVisitDoctorAdviceInfo.keySet()) {
					Object value = hospitalVisitDoctorAdviceInfo.get(key);
					if(value instanceof String){
						String dealValue = null;
						try {
							dealValue = new String(((String)value).getBytes("ISO8859-1"),"gbk");
						} catch (UnsupportedEncodingException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						hospitalVisitDoctorAdviceInfo.put(key, dealValue);
					}
				}
				String detpCode = (String)hospitalVisitDoctorAdviceInfo.get("DEPTCODE");
				HospitalVisitDoctorAdviceTemporaryRecord doctorAdviceTemporaryRecord = null;
				if(doctorAdviceTemporaryRecordMap.containsKey(detpCode)){
					doctorAdviceTemporaryRecord = doctorAdviceTemporaryRecordMap.get(detpCode);
				}else{
					doctorAdviceTemporaryRecord = new HospitalVisitDoctorAdviceTemporaryRecord();
					doctorAdviceTemporaryRecord.setMrId(basicInfo.getMrId());
					doctorAdviceTemporaryRecord.setPatientName(basicInfo.getPatientName());
					doctorAdviceTemporaryRecordMap.put(detpCode, doctorAdviceTemporaryRecord);
				}
				
				HospitalVisitDoctorAdviceTemporaryItem doctorAdviceTemporaryItem = new HospitalVisitDoctorAdviceTemporaryItem();
				
				String presupposeDateTimeStr = (String)hospitalVisitDoctorAdviceInfo.get("PRESUPPOSEDATETIME");
				if(presupposeDateTimeStr != null){
					try {
						Date presupposeDateTime = DateUtil.dateParse(DateUtil.DATE_TIME_FORMATE,presupposeDateTimeStr);
						doctorAdviceTemporaryItem.setPresupposeDateTime(presupposeDateTime);
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
				StringBuilder sbDoctorAdviceContent = new StringBuilder();
				
				String orderText = (String)hospitalVisitDoctorAdviceInfo.get("ORDER_TEXT");
				if(orderText !=null){
					sbDoctorAdviceContent.append(orderText);
				}
				
				BigDecimal dosag = (BigDecimal)hospitalVisitDoctorAdviceInfo.get("DOSAGE");
				
				
				String dosagUnits = (String)hospitalVisitDoctorAdviceInfo.get("DOSAGE_UNITS");
				if(dosag !=null){
					sbDoctorAdviceContent.append("   "+dosag+dosagUnits);
				}
				
				String administration = (String)hospitalVisitDoctorAdviceInfo.get("ADMINISTRATION");
				if(administration !=null){
					sbDoctorAdviceContent.append("   "+administration);
				}
				doctorAdviceTemporaryItem.setDoctorAdviceContent(sbDoctorAdviceContent.toString());
				doctorAdviceTemporaryItem.setDoctorName((String)hospitalVisitDoctorAdviceInfo.get("DOCTORNAME"));
				doctorAdviceTemporaryItem.setNurseName((String)hospitalVisitDoctorAdviceInfo.get("NURSENAME"));
				String executionDateTimeStr = (String)hospitalVisitDoctorAdviceInfo.get("EXECUTIONDATETIME");
				if(executionDateTimeStr != null){
					try {
						Date executionDateTime = DateUtil.dateParse(DateUtil.DATE_TIME_FORMATE,executionDateTimeStr);
						doctorAdviceTemporaryItem.setExecutionDateTime(executionDateTime);
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				doctorAdviceTemporaryRecord.getDoctorAdviceTemporaryItems().add(doctorAdviceTemporaryItem);
			}
			for (HospitalVisitDoctorAdviceTemporaryRecord hospitalVisitDoctorAdviceTemporaryRecord : doctorAdviceTemporaryRecordMap.values()) {
				doctorAdviceTemporaryInfo.getDoctorAdviceTemporaryRecords().add(hospitalVisitDoctorAdviceTemporaryRecord);
			}
			
			
			List<Map<String, Object>> hospitalVisitExaminationReportInfos = exportDataMapper.getHospitalVisitExaminationReportInfoByOracle(basicInfo.getMrId(),basicInfo.getVisitNumber().intValue());
			
			HospitalVisitExaminationReportsInfo examinationReportsInfo = new HospitalVisitExaminationReportsInfo();
			examinationReportsInfo.setVisitGuid(visitGuid);
			for (Map<String, Object> hospitalVisitExaminationReportInfo : hospitalVisitExaminationReportInfos) {
				for (String key : hospitalVisitExaminationReportInfo.keySet()) {
					Object value = hospitalVisitExaminationReportInfo.get(key);
					if(value instanceof String){
						String dealValue = null;
						try {
							dealValue = new String(((String)value).getBytes("ISO8859-1"),"gbk");
						} catch (UnsupportedEncodingException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						hospitalVisitExaminationReportInfo.put(key, dealValue);
					}
				}
				HospitalVisitExaminationReportsRecord examinationReportsRecord = new HospitalVisitExaminationReportsRecord();
				String applyNo = (String)hospitalVisitExaminationReportInfo.get("APPLYNO");
				examinationReportsRecord.setPatientName((String)hospitalVisitExaminationReportInfo.get("PATIENTNAME"));
				examinationReportsRecord.setDeptCategoryName((String)hospitalVisitExaminationReportInfo.get("DEPTCATEGORYNAME"));
				examinationReportsRecord.setSexName((String)hospitalVisitExaminationReportInfo.get("SEXNAME"));
				BigDecimal age = (BigDecimal)hospitalVisitExaminationReportInfo.get("AGENAME");
				if(age != null){
					examinationReportsRecord.setAgeName(age.intValue()+"");
				}
				examinationReportsRecord.setSpecimenTypeName((String)hospitalVisitExaminationReportInfo.get("SPECIMENTYPENAME"));
				examinationReportsRecord.setSubmittingPhysicianName((String)hospitalVisitExaminationReportInfo.get("SUBMITTINGPHYSICIANNAME"));
				examinationReportsRecord.setClinicalDiagnosisName((String)hospitalVisitExaminationReportInfo.get("CLINICALDIAGNOSISNAME"));
				
				String samplingDateTimeStr = (String)hospitalVisitExaminationReportInfo.get("SAMPLINGDATETIME");
				if(samplingDateTimeStr != null){
					try {
						Date samplingDateTime = DateUtil.dateParse(DateUtil.DATE_TIME_FORMATE,samplingDateTimeStr);
						examinationReportsRecord.setSamplingDateTime(samplingDateTime);
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
				String inspectDateTimeStr = (String)hospitalVisitExaminationReportInfo.get("INSPECTDATETIME");
				if(inspectDateTimeStr != null){
					try {
						Date inspectDateTime = DateUtil.dateParse(DateUtil.DATE_TIME_FORMATE,inspectDateTimeStr);
						examinationReportsRecord.setInspectDateTime(inspectDateTime);
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				examinationReportsRecord.setInspectionDoctorName((String)hospitalVisitExaminationReportInfo.get("INSPECTIONDOCTORNAME"));
				
				String acceptanceCheckDateTimeStr = (String)hospitalVisitExaminationReportInfo.get("ACCEPTANCECHECKDATETIME");
				if(acceptanceCheckDateTimeStr != null){
					try {
						Date acceptanceCheckDateTime = DateUtil.dateParse(DateUtil.DATE_TIME_FORMATE,acceptanceCheckDateTimeStr);
						examinationReportsRecord.setAcceptanceCheckDateTime(acceptanceCheckDateTime);
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
				String reportDateTimeStr = (String)hospitalVisitExaminationReportInfo.get("REPORTDATETIME");
				if(reportDateTimeStr != null){
					try {
						Date reportDateTime = DateUtil.dateParse(DateUtil.DATE_TIME_FORMATE,reportDateTimeStr);
						examinationReportsRecord.setReportDateTime(reportDateTime);
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				examinationReportsRecord.setAuditorDoctorName((String)hospitalVisitExaminationReportInfo.get("AUDITORDOCTORNAME"));
				
				examinationReportsInfo.getExaminationReportsRecords().add(examinationReportsRecord);
				List<Map<String, Object>> hospitalVisitExaminationItems = exportDataMapper.getHospitalVisitExaminationItemInfoByOracle(applyNo);
				for (Map<String, Object> hospitalVisitExaminationItem : hospitalVisitExaminationItems) {
					for (String key : hospitalVisitExaminationItem.keySet()) {
						Object value = hospitalVisitExaminationItem.get(key);
						if(value instanceof String){
							String dealValue = null;
							try {
								dealValue = new String(((String)value).getBytes("ISO8859-1"),"gbk");
							} catch (UnsupportedEncodingException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							hospitalVisitExaminationItem.put(key, dealValue);
						}
					}
					HospitalVisitExaminationReportsItem examinationReportsItem = new HospitalVisitExaminationReportsItem();
					examinationReportsItem.setProjectName((String)hospitalVisitExaminationItem.get("PROJECTNAME"));
					examinationReportsItem.setExaminationResult((String)hospitalVisitExaminationItem.get("EXAMINATIONRESULT"));
					examinationReportsItem.setExaminationUnit((String)hospitalVisitExaminationItem.get("EXAMINATIONUNIT"));
					examinationReportsItem.setExaminationResultMark((String)hospitalVisitExaminationItem.get("EXAMINATIONRESULTMARK"));
					examinationReportsRecord.getExaminationReportsItems().add(examinationReportsItem);
				}
			}
			
			hospitalVisitFillInfo.put("basicInfo", basicInfo);
			hospitalVisitFillInfo.put("cureInfo", cureInfo);
			hospitalVisitFillInfo.put("diseaseDiagInfo", diseaseDiagInfo);
			hospitalVisitFillInfo.put("operateInfo", operateInfo);
			hospitalVisitFillInfo.put("transferDeptInfo", transferDeptInfo);
			hospitalVisitFillInfo.put("nurseInfo", nurseInfo);
			hospitalVisitFillInfo.put("intensiveCareInfo", intensiveCareInfo);
			hospitalVisitFillInfo.put("infusionBloodInfo", infusionBloodInfo);
			hospitalVisitFillInfo.put("pressureSoreInfo", pressureSoreInfo);
			hospitalVisitFillInfo.put("doctorAdviceTemporaryInfo", doctorAdviceTemporaryInfo);
			hospitalVisitFillInfo.put("examinationReportsInfo", examinationReportsInfo);
			
			hospitalVisitFillInfo.put("visitCostInfo", visitCostInfo);
			hospitalVisitFillInfos.add(hospitalVisitFillInfo);
		}
		return hospitalVisitFillInfos;
	}

	@TargetDataSource(name="ds1")
	@Transactional
	@Override
	public void insertHospitalVisitRecords(List<Map<String, Object>> datas) {
		// TODO Auto-generated method stub
		for (Map<String, Object> data : datas) {
			insertHospitalVisitRecord(data);
		}
	}
	
	public int insertHospitalVisitRecord(Map<String, Object> data){
		HospitalVisitBasicInfo basicInfo = (HospitalVisitBasicInfo)data.get("basicInfo");
		CitizensInfo patient = new CitizensInfo();
		patient.setGuid(basicInfo.getVisitGuid());
		patient.setName(basicInfo.getPatientName());
		List<Map<String,Object>> sexs = exportDataMapper.getSexIdsByName(basicInfo.getSexName());
		if(!sexs.isEmpty()){
			patient.setSexId((Integer)sexs.get(0).get("id"));
		}
		
		patient.setBirthday(basicInfo.getBirthday());
		List<Map<String,Object>> idDocumentTypes = exportDataMapper.getIdDocumentTypeByCode(basicInfo.getIdDocumentTypeCode());
		if(!idDocumentTypes.isEmpty()){
			patient.setIdType((Integer)idDocumentTypes.get(0).get("id"));
		}
		
		patient.setIdNumber(basicInfo.getIdNumber());
		patient.setStatus(1);
		patient.setCreateDate(new Date());
		patient.setUpdateDate(new Date());
		
		FirstLevelMedical firstLevelMedical = new FirstLevelMedical();
		firstLevelMedical.setCitizensGuid(patient.getGuid());
		List<Map<String, Object>> hospitals = exportDataMapper.getHospitalByCode(basicInfo.getHospitalCode());
		if(!hospitals.isEmpty()){
			firstLevelMedical.setHospitalId((Integer)hospitals.get(0).get("id"));
			basicInfo.setHospitalName((String)hospitals.get(0).get("name"));
		}
		firstLevelMedical.setGuid(StringUtil.getId());
		firstLevelMedical.setTreatmentSignId(1);
		firstLevelMedical.setTreatmentTypeId(1);
		firstLevelMedical.setCreateDate(new Date());
		firstLevelMedical.setUpdateDate(new Date());
		firstLevelMedical.setStatus(1);
		
		HospitalizationNote note = new HospitalizationNote();
		note.setFirstLevelMedicalGuid(firstLevelMedical.getGuid());
		
		note.setInHospitalDate(basicInfo.getInHospitalDateTime());
		note.setVisitNumber(basicInfo.getVisitNumber().intValue());
		
		
		note.setOutHospitalDate(basicInfo.getOutHospitalDateTime());
		
		List<Map<String,Object>> inMedicalOffices = exportDataMapper.getOfficesByOfficeCode(basicInfo.getInDeptCode());
		if(!inMedicalOffices.isEmpty()){
			note.setInDeptId((Integer)inMedicalOffices.get(0).get("id"));
		}
		
		List<Map<String,Object>> outMedicalOffices = exportDataMapper.getOfficesByOfficeCode(basicInfo.getOutDeptCode());
		if(!outMedicalOffices.isEmpty()){
			note.setOutDeptId((Integer)outMedicalOffices.get(0).get("id"));
		}
		note.setMrId(basicInfo.getMrId());
		note.setOnlyId(basicInfo.getOnlyId());
		int count = hospitalizationNoteMapper.getHospitalizationNoteCountByMrIdAndOnlyId(basicInfo.getMrId(),basicInfo.getVisitNumber().intValue());
		if(count > 0){
			return 1;
		}
		patientMapper.insert(patient);
		firstLevelMedicalMapper.insert(firstLevelMedical);
		
		List<Map<String,Object>> inHospitalTypes = exportDataMapper.getMysqlInHospitalTypeByCode(basicInfo.getInHospitalTypeCode());
		
		if(!inHospitalTypes.isEmpty()){
			note.setInHospitalTypeId((Integer)inHospitalTypes.get(0).get("id"));
		}
		
		List<Map<String,Object>> outHospitalTypes = exportDataMapper.getOutHospitalTypeByCode(basicInfo.getOutHospitalTypeCode());
		
		if(!outHospitalTypes.isEmpty()){
			note.setOutHospitalTypeId((Integer)outHospitalTypes.get(0).get("id"));
		}
		hospitalizationNoteMapper.insert(note);
		String filePath = myConfig.getHospitalVisitJsonPath() + StringUtil.getLocalPath(basicInfo.getVisitGuid());
		String versionFilePath = myConfig.getHospitalVisitJsonVersionPath()+ StringUtil.getLocalPath(basicInfo.getVisitGuid());
		String fileName = basicInfo.getVisitGuid() + ".json";
		String versionFileName = basicInfo.getVisitGuid()+"-" + DateUtil.dateFormat("yyyyMMddHHmmssssss", new Date()) + ".json";
		
		String jsonMapJson = JsonUtil.objectToJson(data);
		try {
			FileUtil.createOrEditFile(jsonMapJson, filePath, fileName);
			FileUtil.createOrEditFile(jsonMapJson, versionFilePath, versionFileName);
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}
	
	@SuppressWarnings("unchecked")
	public Map<String, Object> readLineFile(String filePath, String fileName){
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		
		try {
			File destFile = new File(filePath,fileName);
			String jsonStr = null;
			if(destFile.exists()){
				 FileReader fr = new FileReader(destFile);
			        BufferedReader br = new BufferedReader(fr);
			        jsonStr = br.readLine();
			        jsonMap = JsonUtil.jsonToPojo(jsonStr, Map.class);
			       
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return jsonMap;
	}
}
